﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BasePlatformNet;
using BasePlatformCommon;

namespace L9SubSet {
    public partial class Form_ChipConfigUCS512D : Form {
        public Form_ChipConfigUCS512D(Form_MainSet main, BaseCoreNet netCore) {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterParent;
            this.netCore = netCore;
            this.parent = main;

            initEnviroment();
            setLanguageText();
        }

        private BaseCoreNet netCore;
        private Form_MainSet parent;

        private string section = "ChipConfigUCS512D";

        private void initEnviroment() {
            cmbChannelNum.Items.Clear();
            for (var i = 0; i < 3;i++ ) {
                var level = 1 << (2 - i);
                cmbChannelNum.Items.Add(string.Format("{0}字段", level));
                if (LanguageOper.available) {
                    var formatStr = LanguageOper.ReadValue(section, "ChannelNum.items.format");
                    cmbChannelNum.Items[i] = string.Format(formatStr, level);
                }
            }
            cmbChannelNum.SelectedIndex = 0;

            cmbNoSignal.Items.Clear();
            cmbNoSignal.Items.Add("保持最后一帧");
            cmbNoSignal.Items.Add("恢复上电亮灯");
            cmbNoSignal.SelectedIndex = 0;

            if (LanguageOper.available) {
                cmbNoSignal.Items[0] = LanguageOper.ReadValue(section, "NoSignal.items.0");
                cmbNoSignal.Items[1] = LanguageOper.ReadValue(section, "NoSignal.items.1");
            }
        }

        private void trackBarR_Scroll(object sender, EventArgs e) {
            tbR.Text = trackBarR.Value.ToString();
        }

        private void tbR_TextChanged(object sender, EventArgs e) {
            var val = 64;
            if (int.TryParse(tbR.Text,out val)) {
                trackBarR.Value = val;
            }
        }

        private void trackBarG_Scroll(object sender, EventArgs e) {
            tbG.Text = trackBarG.Value.ToString();
        }

        private void tbG_TextChanged(object sender, EventArgs e) {
            var val = 64;
            if (int.TryParse(tbG.Text, out val)) {
                trackBarG.Value = val;
            }
        }

        private void trackBarB_Scroll(object sender, EventArgs e) {
            tbB.Text = trackBarB.Value.ToString();
        }

        private void tbB_TextChanged(object sender, EventArgs e) {
            var val = 64;
            if (int.TryParse(tbB.Text, out val)) {
                trackBarB.Value = val;
            }
        }

        private void trackBarW_Scroll(object sender, EventArgs e) {
            tbW.Text = trackBarW.Value.ToString();
        }

        private void tbW_TextChanged(object sender, EventArgs e) {
            var val = 64;
            if (int.TryParse(tbW.Text, out val)) {
                trackBarW.Value = val;
            }
        }

        private void btnApply_Click(object sender, EventArgs e) {
            var data = getData();
            if (data == null) {
                return;
            }
            parent.SetChipConfig(data);
            parent.DmxSaveButtonClick();
        }

        private byte[] getData() {
            var data = new byte[32];
            var val = 0;
            val |= cmbChannelNum.SelectedIndex << 6;
            val |= cmbNoSignal.SelectedIndex << 5;
            val |= 0x2;
            data[0] = (byte)val;
            data[1] = (byte)(trackBarR.Value - 1);
            data[2] = (byte)(trackBarG.Value - 1);
            data[3] = (byte)(trackBarB.Value - 1);
            data[4] = (byte)(trackBarW.Value - 1);
            return data;
        }

        private void setLanguageText() {
            if (!LanguageOper.available) {
                return;
            }
            this.Text = LanguageOper.ReadValue(section, "ChipConfigUCS512D.Text");
            btnApply.Text = LanguageOper.ReadValue(section, "btnApply.Text");
            grpChannelNum.Text = LanguageOper.ReadValue(section, "grpChannelNum.Text");
            grpNoSignal.Text = LanguageOper.ReadValue(section, "grpNoSignal.Text");
            grpColor.Text = LanguageOper.ReadValue(section, "grpColor.Text");
        }
    }
}
